<?php

namespace App\Admin\Controllers;

use App\Models\Messages;
use App\Models\News;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NewsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '新闻列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new News());

        $grid->column('id', '编号')->sortable();
        $grid->column('title', '标题')->text();
        $grid->column('author', '作者')->text();
        $grid->column('watch', '浏览量')->text();
        $grid->column('created_at', '创建时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->toDateTimeString();
        });
        $grid->column('updated_at', '修改时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->toDateTimeString();
        });

        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Messages::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new News);

        $form->text('title', '标题')->required();
        $form->text('author', '作者')->required();
        $form->number('watch', '浏览量')->min(0)->default(0)->required();

        // 使用内置的富文本编辑器
        $form->editor('content', '内容')->required();

        // 自定义表单工具
        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });

        // 自定义表单底部
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
